package com.xintonghua.bussiness.ui.fragment.cube;

import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.GoodAdapter;
import com.xintonghua.bussiness.adapter.RecordAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.GoodsItemBeaan;
import com.xintonghua.bussiness.bean.GoodsItemResponse;
import com.xintonghua.bussiness.bean.GoodsManageResponse;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 进货记录  销货记录
 * 作者：wanxianyi
 * 时间：2018/4/25.
 * 包名：com.xintonghua.bussiness.ui.fragment.cube
 */

public class RecordActivity extends BaseActivity {
    @BindView(R.id.lv_record)
    XRecyclerView lvRecord;
    private int page = 1;
    private String name;
    private RecordAdapter adapter;
    GoodsItemResponse response;
    List<GoodsItemBeaan> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        ButterKnife.bind(this);
        onSimpleActionBar();
        name = getIntent().getStringExtra(BuildConfig.FLAVOR);
        setSimpleActionBar(name + "记录");
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        RefreshUtils.initList(this,lvRecord);
        adapter = new RecordAdapter(this, list, name);
        lvRecord.setAdapter(adapter);
        lvRecord.setLoadingListener(this);
        find();
    }

    private void find() {
        if (name.equals("进货")) {
            httpCent.jinhuoDetail(page, this, MyState.requestOne);
        } else {
            httpCent.xiaoliangDetail(page, this, MyState.requestOne);
        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    list.clear();
                }
                response = JSONObject.parseObject((String) data, GoodsItemResponse.class);
                list.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvRecord);
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvRecord)) {
            page++;
            find();
        }
    }
}
